package work20241126_27;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author zdl
 * @date 2024/11/26
 */
public class Demo17 {
    public static void main(String[] args) {
        //实现一个方法，查找数组中某个元素的最后一个索引。
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数:(1-4)");
        int num = sc.nextInt();
        lastIndex(num);
    }
    public static void lastIndex(int num){
        int[] arr = {1,2,3,4,1,2,3,4,1,2,1,4};
        int[] indexArr = new int[arr.length];
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (num == arr[i]){
                indexArr[count++] = i;
            }
        }
        int[] new_arr = new int[count];
        for (int i = 0; i < new_arr.length; i++) {
            new_arr[i] = indexArr[i];
        }
        System.out.println(num+"在数组中所有的下标位置："+Arrays.toString(new_arr));
        System.out.println("数组中"+num+"最后一个索引下标为:"+new_arr[new_arr.length-1]);
    }
}
